package com.miyu.client1.service.magic.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.miyu.client1.dao.book.bookMagicDao;
import com.miyu.client1.pojo.book.BookListInfo;
import com.miyu.client1.utils.ehcache.EhcacheUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Component
public class saveData implements Pipeline {
    @Autowired
    private bookMagicDao bookMagicDao;

    private static saveData service;

    @PostConstruct
    public void  init(){
        service = this;
        service.bookMagicDao = this.bookMagicDao;
    }

    @Override
    public void process(ResultItems resultItems, Task task) {


        if(resultItems.get("state").equals("main")){
            JSONArray info = resultItems.get("searchInfo");
            JSONArray jsonArray= JSONArray.parseArray(JSONObject.toJSONString(EhcacheUtil.getInstance().get("bookEhcache","bookCache")));
            if(jsonArray==null){
                JSONArray ja = new JSONArray();
                ja.addAll(info);
                EhcacheUtil.getInstance().put("bookEhcache", "bookCache", ja);
            }else{
                jsonArray.addAll(info);
                EhcacheUtil.getInstance().put("bookEhcache", "bookCache", jsonArray);
            }
        }else if( resultItems.get("state").equals("detail")) {
            //表头数据
            JSONObject head = resultItems.get("headInfo");
            EhcacheUtil.getInstance().put("bookEhcache", "headTitleInfo", head);
            //书籍章节+内容保存
            JSONArray jsonArray= JSONArray.parseArray(JSONObject.toJSONString(EhcacheUtil.getInstance().get("bookEhcache","detail")));
            JSONArray info = resultItems.get("info");
            if(jsonArray==null){
                JSONArray ja = new JSONArray();
                ja = info;
                //ja.add(JSONObject.parse(info));
                EhcacheUtil.getInstance().put("bookEhcache", "detail", ja);
            }else{
               // jsonArray.add(JSONObject.parse(info));
                EhcacheUtil.getInstance().put("bookEhcache", "detail", jsonArray);
            }


        }else if( resultItems.get("state").equals("update")) {
            //书籍章节+内容保存
            JSONObject jo = (JSONObject) EhcacheUtil.getInstance().get("bookEhcache","detail");
            String info = resultItems.get("info");
            jo = JSONObject.parseObject(info);
            if(jo==null){
                EhcacheUtil.getInstance().put("bookEhcache", "chapter", jo);
            }else{
                EhcacheUtil.getInstance().put("bookEhcache", "chapter", jo);
            }
        }else if(resultItems.get("state").equals("downFile")){
            //下载内容缓存
            JSONArray info = resultItems.get("info");
            JSONArray jsonArray= JSONArray.parseArray(JSONObject.toJSONString(EhcacheUtil.getInstance().get("bookEhcache","downFile")));
            if(jsonArray==null){
                JSONArray ja = new JSONArray();
                ja.addAll(info);
                EhcacheUtil.getInstance().put("bookEhcache", "downFile", ja);
            }else{
                jsonArray.addAll(info);
                EhcacheUtil.getInstance().put("bookEhcache", "downFile", jsonArray);
            }

        }

    }
    private  String UUID(){
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        return uuid;
    }
}
